package com.app.teaware.ware;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.teaware.R;
import com.app.teaware.basis.BasisActivity;
import com.app.teaware.basis.Constants;
import com.app.teaware.bean.GoodsListData;
import com.app.teaware.bean.GoodsWorks;
import com.app.teaware.bean.GoodsWorksListBean;
import com.app.teaware.bean.LoginBean;
import com.app.teaware.util.MyUtils;
import com.app.teaware.util.OnClickUtils;
import com.app.teaware.util.ReceiverUtils;
import com.app.teaware.util.UHFUtil;

import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import my.http.MyHttpListener;
import my.http.MyHttpUtils;
import my.http.MyParams;

/**
 * 批量核验
 */
public class AllCheckListActivity extends BasisActivity {


    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;

    GoodsWorksListBean listBean;

    MyAdapterList mAdapter;


    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.all_check_activity);
        setTitle("批量核验");
        setTitleLeftButton(null);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
    }

    private BroadcastReceiver mResultReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if(!Constants.ACTION_UHF_RESULT_SEND.equals(action))
                return ;
            Parcelable[] tagInfos =  intent.getParcelableArrayExtra(Constants.EXTRA_TAG_INFO);
            String qrcode = UHFUtil.getEPCId(tagInfos).trim().toUpperCase(Locale.ROOT);

            if(!TextUtils.isEmpty(qrcode)) {
                listBean.haveRfid(qrcode,mAdapter);
            }
        }//end onReceiver
    };

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        listBean = new GoodsWorksListBean();
        setListView();
    }


    private void getList(){
        showLoading();
        MyParams params = new MyParams();
        params.put("aid",LoginBean.getInstance().getAcode());
        params.put("rfids",listBean.getRfidListString());
        MyHttpUtils.post(Constants.URL_WORK_POST_GET_BATCH, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        if (isFinishing()) return;
                        listBean = (GoodsWorksListBean) result;
                        setListView();
                    }

                    @Override
                    public void onFailure(int httpWhat, Object result) {
                        if (isFinishing()) return;
                        super.onFailure(httpWhat, result);
                        setListView();
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        if (isFinishing()) return;
                        dismissProgress();
                    }
                },
                0, GoodsWorksListBean.class);
    }


    private void setListView() {
        if (mAdapter == null) {
            mAdapter = new MyAdapterList();
            mRecyclerView.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
    }


    private int choosePosition;
    public class MyAdapterList extends RecyclerView.Adapter<MyAdapterList.ViewHolder> {
        public MyAdapterList() {

        }

        //创建新View，被LayoutManager所调用
        @Override
        public MyAdapterList.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.ware_check_list_item, viewGroup, false);
            return new MyAdapterList.ViewHolder(view);
        }

        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(MyAdapterList.ViewHolder viewHolder, int position) {
            viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            int count = listBean == null ? 0 : listBean.getList().size();
            return count;
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.text_rfid)
            public TextView text_rfid;
            @BindView(R.id.text_gna)
            public TextView text_gna;
            @BindView(R.id.text_wms)
            public TextView text_wms;
            @BindView(R.id.text_gcd)
            public TextView text_gcd;
            @BindView(R.id.text_gno)
            public TextView text_gno;
            @BindView(R.id.text_gsp)
            public TextView text_gsp;
            @BindView(R.id.text_gwtp)
            public TextView text_gwtp;
            @BindView(R.id.text_gnu)
            public TextView text_gnu;
            @BindView(R.id.btn_delete)
            public TextView btn_delete;
            @BindView(R.id.btn_edit)
            public TextView btn_edit;


            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            public void bind(int position) {
                GoodsWorks data = listBean.getList().get(position);
                MyUtils.setNotNullText(text_rfid,data.getRfid());
                MyUtils.setNotNullText(text_gna,data.getGna());
                MyUtils.setNotNullText(text_wms,data.getWms());
                MyUtils.setNotNullText(text_gcd,data.getGcd());
                MyUtils.setNotNullText(text_gno,data.getGno());
                MyUtils.setNotNullText(text_gsp,data.getGsp());
                MyUtils.setNotNullText(text_gwtp,""+data.getGwtp());
                MyUtils.setNotNullText(text_gnu,""+data.getGnu());
                if(data.isEmpty()){
                    list_item.setBackground(getResources().getDrawable(R.drawable.base_btn_green_round));
                }else {
                    list_item.setBackground(getResources().getDrawable(R.drawable.base_white_round));
                }
                list_item.setTag(position);
                btn_delete.setTag(position);
                btn_edit.setTag(position);

            }

            @OnClick(R.id.btn_edit)
            void onItemEditClick(View view){
                    // 进行点   击事件后的逻辑操作
                    int position = (int) view.getTag();
                   choosePosition = position;
                    GoodsWorks data = listBean.getList().get(position);
                    ResetWareInfoActivity.toEdit(mContext,data.getRfid(),data,Constants.BASESULT);
            }
        }
    }


    @Override
    protected void onPause() {
        super.onPause();
        ReceiverUtils.unRegisterReceiver(mContext,mResultReceiver);
    }


    @Override
    protected void onResume() {
        super.onResume();
        ReceiverUtils.registerUHFReceiver(mContext,mResultReceiver);
    }


    @OnClick({R.id.btn_clear,R.id.btn_submit})
    public void click(View view){
        switch (view.getId()){
            case R.id.btn_clear:
                listBean.getList().clear();
                mAdapter.notifyDataSetChanged();
                break;
            case R.id.btn_submit:
                getList();
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Constants.BASESULT) {
            if(data.getSerializableExtra("bean")!=null){
                GoodsWorks goodsWorks = (GoodsWorks) data.getSerializableExtra("bean");
                listBean.getList().set(choosePosition,goodsWorks);
                mAdapter.notifyItemChanged(choosePosition);
            }
        }
    }

}
